from common.exception.base_exception import BaseBizException
from common.exception.biz_exception import BizException
from common.exception.biz_error_type import BizErrorType
from jsonschema import validate


class AssertUtils:

    @staticmethod
    def not_null(obj, error_type: BizErrorType = BizErrorType.PARAM_ILLEGAL):
        if obj is None:
            error_type.msg = 'obj cannot be null'
            raise BizException(error_type)

    @staticmethod
    def not_blank(s: str, error_type: BizErrorType = BizErrorType.PARAM_ILLEGAL):
        if s is None or len(s) == 0:
            error_type.msg = 'str cannot be blank'
            raise BizException(error_type)

    @staticmethod
    def not_empty_list(l: list, error_type: BizErrorType = BizErrorType.PARAM_ILLEGAL):
        if l is None or len(l) == 0:
            error_type.msg = 'list cannot be empty'
            raise BizException(error_type)

    @staticmethod
    def not_empty_namespace(ns, error_type: BizErrorType = BizErrorType.PARAM_ILLEGAL):
        for value in ns.__dict__.values():
            if value is not None:
                return
        error_type.msg = 'Namespace obj is empty'
        raise BizException(error_type)

    @staticmethod
    def validate_json_schema(data, schema):
        validate(data, schema)
